
//
//  ZZJinJiaView.m
//  新项目
//
//  Created by 刘耀宗 on 2017/3/16.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZJinJiaView.h"
@interface ZZJinJiaView ()<UITextFieldDelegate>
@property (nonatomic,strong) UIDatePicker *datePicker;
@property (nonatomic,strong) UIDatePicker *datePicker1;
@property(nonatomic,strong) UIToolbar *toolBar;
@property (nonatomic, assign) NSInteger tagNum;
@property (weak, nonatomic) IBOutlet UITextField *GHTextField;
@property(nonatomic,strong) UIToolbar *toolBar1;
@end
@implementation ZZJinJiaView
+(instancetype)ViewWithXib
{
    ZZJinJiaView *view=[[[NSBundle mainBundle] loadNibNamed:@"ZZJinJiaView" owner:nil options:nil]lastObject];
    return view;
    
}
- (IBAction)clickForSeletecd:(id)sender {
    if ([self.delegate respondsToSelector:@selector(clickForSeletectedDataWithStime:andEndTime:andName:)]) {
        if (self.GHTextField.text.length==0||self.GHTextField.text==nil||[[self.GHTextField.text class] isSubclassOfClass:[NSNull class]]) {
            self.GHTextField.text=@"";
        }
        [self.delegate clickForSeletectedDataWithStime:self.startTimeField.text andEndTime:self.endTimeField.text andName:self.GHTextField.text];
        
    }
}
//隐藏
- (IBAction)clickForHiden:(id)sender {
    if ([self.delegate respondsToSelector:@selector(hiddenTheView)]) {
        [self.delegate hiddenTheView];
    }
}


-(void)awakeFromNib
{
    [super awakeFromNib];
    self.startTimeField.delegate=self;
    self.endTimeField.delegate=self;
    NSDate *date=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    formatter.dateFormat=@"yyyy-MM-dd";
    NSString *STR=[formatter stringFromDate:date];
    self.endTimeField.text=STR;
    
    NSTimeInterval time=24*60*60*31;
    NSDate *date1=[date dateByAddingTimeInterval:-time];
    NSString *STR2=[formatter stringFromDate:date1];
    self.startTimeField.text=STR2;
    
    
}
#pragma mark - 监听按钮的点击事件
- (void)complete{
    
    [self endEditing:YES];
    //隐藏
//    [self compareStartTimeAndEndTime];
    
    
    
}
//比较当前的开始时间和结束时间
-(BOOL)compareStartTimeAndEndTime
{
    NSString *startTime=self.startTimeField.text;
    NSArray *startArr=[startTime componentsSeparatedByString:@"-"];
    //年份
    NSInteger startNian=[startArr[0] integerValue];
    NSInteger startYue=[startArr[1] integerValue];
    NSInteger startRi=[startArr[2] integerValue];
    NSString *endTime=self.endTimeField.text;
    NSArray *endArr=[endTime componentsSeparatedByString:@"-"];
    //年份
    NSInteger endNian=[endArr[0] integerValue];
    NSInteger endYue=[endArr[1] integerValue];
    NSInteger endRi=[endArr[2] integerValue];
    
    if (startNian>endNian) {
        [self showError];
    }else
    {
        if (startYue>endYue) {
            [self showError];
            
        }else
        {
            if (startRi>endRi) {
                [self showError];
                
            }else
            {
                //                if ([self.delegate respondsToSelector:@selector(chooseTimeWithStartTime:andEndTime:)]) {
                //                    [self.delegate chooseTimeWithStartTime:self.startTimeField.text andEndTime:self.endTimeField.text];
                //                }
                
            }
            
        }
        
        
        
        
    }
    
    
    
    
    
    return YES;
    
    
    
}
-(void)showError
{
    NSDate *date=[NSDate date];
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    formatter.dateFormat=@"yyyy-MM-dd";
    NSString *STR=[formatter stringFromDate:date];
    self.endTimeField.text=STR;
    
    NSTimeInterval time=24*60*60*31;
    NSDate *date1=[date dateByAddingTimeInterval:-time];
    NSString *STR2=[formatter stringFromDate:date1];
    self.startTimeField.text=STR2;
    
    [MBProgressHUD showError:@"时间区间选择错误"];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        // 隐藏登录指示器
        [MBProgressHUD hideHUD];
    });
    
}
#pragma mark -  开始编辑
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if ([textField.placeholder isEqualToString:@"开始时间不能大于结束时间"]) {
        self.tagNum=200;
        
    }else if ([textField.placeholder isEqualToString:@"结束时间不能低于开始时间"])
    {
        self.tagNum=201;
        
    }
    
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    
    
    
    
    if ([textField.placeholder isEqualToString:@"开始时间不能大于结束时间"]) {
        textField.inputAccessoryView=self.toolBar;
        textField.inputView=self.datePicker;
        
    }else if ([textField.placeholder isEqualToString:@"结束时间不能低于开始时间"])
    {
        textField.inputAccessoryView=self.toolBar1;
        textField.inputView=self.datePicker1;
    }
    return YES;
}


- (UIToolbar *)toolBar {
    if (_toolBar == nil) {
        // 1.创建键盘工具条
        UIToolbar *toolbar = [[UIToolbar alloc] init];
        toolbar.frame = CGRectMake(0, 0, 320, 44);
        toolbar.barTintColor = blueColor;
        toolbar.tintColor = [UIColor whiteColor];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 4. 完成
        UIBarButtonItem *complete = [[UIBarButtonItem alloc] initWithTitle:@"确定" style: UIBarButtonItemStyleDone target:self action:@selector( complete)];
        UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"选择结束时间" style: UIBarButtonItemStyleDone target:self action:@selector(chooseEndTime)];
        toolbar.items = @[flexibleSpace,next,complete];
        _toolBar = toolbar;
    }
    return _toolBar;
}
-(void)chooseEndTime
{
    [self.endTimeField becomeFirstResponder];
}
- (UIToolbar *)toolBar1 {
    if (_toolBar1 == nil) {
        // 1.创建键盘工具条
        UIToolbar *toolbar = [[UIToolbar alloc] init];
        toolbar.frame = CGRectMake(0, 0, 320, 44);
        toolbar.barTintColor = blueColor;
        toolbar.tintColor = [UIColor whiteColor];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        
        // 4. 完成
        UIBarButtonItem *complete = [[UIBarButtonItem alloc] initWithTitle:@"确定" style: UIBarButtonItemStyleDone target:self action:@selector( complete)];
        UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"选择开始时间" style: UIBarButtonItemStyleDone target:self action:@selector( chooseStartTime)];
        toolbar.items = @[flexibleSpace,next,complete];
        _toolBar1 = toolbar;
    }
    return _toolBar1;
}
-(void)chooseStartTime
{
    [self.startTimeField becomeFirstResponder];
    
}

#pragma mark - 懒加载控件
- (UIDatePicker *)datePicker {
    
    DLog(@"创建了日期选择器");
    // 创建日期选择器
    // 一般不需要设置宽高
    _datePicker = [[UIDatePicker alloc] init];
    // 设置时区
    _datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    _datePicker.datePickerMode = UIDatePickerModeDate;
    // 设置最小选择时间
    //        _datePicker.minimumDate =[NSDate date];
    //[NSDate dateWithTimeIntervalSinceNow:-(365 * 20 * 24 * 3600)];
    //获取当前的值
    NSDateFormatter *matter=[[NSDateFormatter alloc] init];
    NSString *time=self.endTimeField.text;
    matter.dateFormat=@"yyyy年MM月dd";
    NSDate *date=[matter dateFromString:time];
    // 设置最大选择时间
    _datePicker.maximumDate = date;
    // 设置时间间隔
    _datePicker.minuteInterval = 2;
    
    // 监听时间选择器值改变时间
    // 提示：除了按钮，其它能够添加事件的控件，一般都是UIControlEventValueChanged事件
    [_datePicker addTarget:self action:@selector(dateValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    return _datePicker;
}
#pragma mark - 懒加载控件
- (UIDatePicker *)datePicker1 {
    
    DLog(@"创建了日期选择器");
    // 创建日期选择器
    // 一般不需要设置宽高
    _datePicker1 = [[UIDatePicker alloc] init];
    // 设置时区
    _datePicker1.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    // 设置时间模式
    _datePicker1.datePickerMode = UIDatePickerModeDate;
    NSDateFormatter *matter=[[NSDateFormatter alloc] init];
    NSString *time=self.startTimeField.text;
    matter.dateFormat=@"yyyy-MM-dd";
    NSDate *date=[matter dateFromString:time];
    // 设置最小选择时间
    _datePicker1.minimumDate =date;
    // 设置最大选择时间
    _datePicker1.maximumDate = [NSDate date];
    
    // 设置时间间隔
    _datePicker1.minuteInterval = 2;
    
    // 监听时间选择器值改变时间
    // 提示：除了按钮，其它能够添加事件的控件，一般都是UIControlEventValueChanged事件
    [_datePicker1 addTarget:self action:@selector(dateValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    
    return _datePicker1;
}

/**
 *  监听日期改变事件
 */
- (void)dateValueChanged:(UIDatePicker *)datePicker {
    // 获得当前显示的时间
    NSDate *date = datePicker == nil? [NSDate date]: datePicker.date;
    // 创建日期格式化对象
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 设置时间格式
    fmt.dateFormat  =@"yyyy-MM-dd";
    // 将日期转换成字符串
    NSString *timeStr =[fmt stringFromDate:date];
    if (self.tagNum==200) {
        self.startTimeField.text=timeStr;
    }else if (self.tagNum==201)
    {
        self.endTimeField.text=timeStr;
    }
    
}


@end
